import { time } from "echarts";

export function ReadingIncrement(params, type) {
  console.log("params1111", params);
  let nodes = [];
  let links = [];
  //遍历数据   将第一条拿出来作为中心，其他的为索引
  if (type === 0) {
    params &&
      params.map((item, index) => {
        //列表页面的关联
        if (item) {
          let obj = {
            id: index,
            name: item
          };

          let obj1 = {
            source: 0,
            target: index,
            name: " "
          };
          nodes.push(obj);
          links.push(obj1);
        }
      });
  } else {
    params &&
      params.map((item, index) => {
        //详情里的关联
        if (item.title) {
          let obj = {
            id: index,
            name: item.title,
            key: item.assetsId,
            // category:index
          };

          let objb = {
            source: 0,
            target: index + 1,
            name: " "
          };
          nodes.push(obj);
          links.push(objb);
        }
      });
  }

  let data = {
    nodes: nodes,
    links: links
  };

  const color1 = "#006acc";
  const color2 = "white";

  // data.nodes.forEach(node => {
  //   if (node.category === 0) {
  //     node.symbolSize = 80;
  //     node.itemStyle = {
  //       color: color1
  //     };
  //   } else {
  //     node.itemStyle = {
  //       color: color2
  //     };
  //   }
  // });

  data.links.forEach(link => {
    link.label = {
      align: "center",
      fontSize: 12
    };
  });

  let categories = [
    //类别
    {
      id: 0,
      itemStyle: {
        color: color1,
        size: 200
      }
    },
    {
      name: "董事",
      itemStyle: {
        color: color2
      }
    }
  ];
  const option = {
    series: [
      {
        type: "graph",
        layout: "force",
        symbolSize: 80,
        draggable: true,
        roam: true,
        focusNodeAdjacency: true,
        categories: categories,
        edgeSymbol: ["", "arrow"],
        // edgeSymbolSize: [80, 10],
        layoutAnimation: true,
        edgeLabel: {
          normal: {
            show: true,
            textStyle: {
              fontSize: 20
            },
            formatter(x) {
              return x.data.name;
            }
          }
        },
        label: {
          show: true
        },
        force: {
          repulsion: 2000,
          edgeLength: 120
        },
        data: data.nodes,
        links: data.links
      }
    ]
  };
  return option;
}
